package com.rom.recursion;

/**
 *@authar: YLNO
 *@date: 2023/11/8-15:31
 *description:
 */
public class MiGong {

    public static void main(String[] args) {
        int[][] map = MiGmap(8);
        //map中间加两个墙
        map[3][1] = 1;
        map[3][2] = 1;
        map[3][3] = 1;
        map[3][4] = 1;
        map[2][4] = 1;
        map[5][5] = 1;
        printMap(map);
        setWay(map,1,1);
        System.out.println("================================================");
        printMap(map);
    }

    /**
     * 路线寻找
     * 策略     下->右->上->左
     * @return
     */
    public static boolean setWay(int[][] map,int i,int j){

        if(map[6][6] == 2){ //通路
            return true;
        }else{
            if (map[i][j] == 0) {
//                没走过假设可以走通
                map[i][j] = 2;
                if (setWay(map, i + 1, j)) { //向下试探
                    return true;
                } else if (setWay(map, i, j + 1)) {
                    return true;
                } else if (setWay(map, i - 1, j)) {
                    return true;
                } else if (setWay(map, i, j - 1)) {
                    return true;
                } else {
                    map[i][j] = 3;
                    return false;
                }
            }
            return  false;
        }
    }
    /**
     * 打印Map
     * @param map
     */
    public static void printMap(int[][] map){
        for (int[] row: map){
            for (int i: row) {
                System.out.printf("\t%d",i);
            }
            System.out.println();
        }
    }

    /**
     * 生成Map
     * @param len
     * @return
     */
    public static int[][] MiGmap(int len){

        int[][] map =  new int[len][len];

        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len; j++) {
                if(i==0 || i == len-1){
                    //第一行和最后一列
                    map[i][j] = 1;
                }else{
//                    其他列
                    if(j==0||j==len-1){
                        map[i][j] = 1;
                    }else{
                        map[i][j] = 0;
                    }
                }
            }
        }
        return map;
    }
}

